package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsWarehouseMapper;
import com.jy.wms.pojo.SysRole;
import com.jy.wms.pojo.SysRoleFunction;
import com.jy.wms.pojo.SysUser;
import com.jy.wms.pojo.WmsWarehouse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @ClassName: WmsWarehouseService.java
* @Description: TODO(这里用一句话描述这个类的作用)
* @Author yjs
* @Date Tue Aug 29 15:39:30 GMT+08:00 2017
*/

@Service
public class WmsWarehouseService extends BaseService<WmsWarehouseMapper,WmsWarehouse> {

 /**
 * 插入一条记录
 * @param entity 实体对象
 * @return int
 */
 @BaseServiceDescribe(moduleName = "仓库管理",btnName = "新增仓库")
public void baseInsert( WmsWarehouse entity) throws ServiceException {
     Integer k=dao.baseInsert( entity );//添加主库仓库信息
     if(k>0){

     }else{
         throw new ServiceException("新增仓库失败");
     }
     /*Integer user=0;
     if(z>0){
         entity.setUser_id(UUIDUtils.uuid());//用户编号
         entity.setUser_sfyx("1");//是否有效 0无效 1有效
         entity.setUser_ssckid(entity.getId());//所属仓库编号
         entity.setUser_lx("1");//用户类型 0系统管理员 1仓库管理员 2仓库操作员 3供应商
         String salt = generateSalt.getSalt();//盐值
         String mm = generateSalt.toPwdSalt("123456", salt);
         entity.setUser_mm(mm);//密码
         entity.setUser_yz(salt);//盐值
         user=dao.baseInsertUser(entity, database_zi);//向子库添加用户
     }else{
         throw new ServiceException("新增仓库失败");
     }
     Integer user2=0;
     if(user>0){
         entity.setUser_sjk(database_zi);//对应数据库名
         user2=dao.baseInsertUsers(entity, database);//向主库添加用户
     }else{
         throw new ServiceException("新增用户失败");
     }
     Integer role=0;
     String role_user = UUIDUtils.uuid();
     if(user2>0){
         role=dao.addRoleUser(role_user, entity.getRole_id(), entity.getUser_id(), database_zi);//子库添加用户角色数据
     }else{
         throw new ServiceException("新增用户失败");
     }
     Integer role2=0;
     if(role>0){
         role2=dao.addRoleUser(role_user, entity.getRole_id(), entity.getUser_id(), database);//主库添加用户角色数据
     }else{
         throw new ServiceException("新增用户角色失败");
     }
     Integer gn=0;
     if(role2>0){
         List<String> ids = entity.getIds();
         List<SysRoleFunction> sysRoleFunction = dao.getFunctionMenu(database);//全部的
         SysRoleMenu sm;
         SysRoleFunction syn = null;
         for (int i = 0; i < sysRoleFunction.size(); i++) {
             for (int j = 0; j < ids.size(); j++) {
                 if (sysRoleFunction.get(i).getGnid().equals(ids.get(j))) {
                     if (sysRoleFunction.get(i).getType().equals("0")) {//菜单
                         sm = new SysRoleMenu();
                         sm.setId(UUIDUtils.uuid());
                         sm.setJsid(entity.getRole_id());
                         sm.setCdid(ids.get(j));
                         gn=dao.addRoleMenu(sm, database_zi);
                         if(gn<0){
                             throw new ServiceException("新增菜单失败");
                         }
                     } else {//功能
                         syn = new SysRoleFunction();
                         syn.setId(UUIDUtils.uuid());
                         syn.setJsid(entity.getRole_id());
                         syn.setGnid(ids.get(j));
                         gn=dao.addSysRoleFunction(syn, database_zi);
                         if(gn<0){
                             throw new ServiceException("新增功能失败");
                         }
                     }
                 }

             }
         }
     }else{
         throw new ServiceException("新增用户角色失败");
     }*/
}
    /**
     * 插入一条用户记录
     * @param entity 实体对象
     * @param database 数据库名称
     * @return int
     */
    /*public Integer baseInsertUser( WmsWarehouse entity, String database ) throws ServiceException,SucessException{
        Integer i = dao.baseInsertUser( entity,database );
        if(i>0){
            *//*throw new SucessException("新增用户成功");*//*
        }else{
            throw new ServiceException("新增用户失败");
        }
    }*/
    /**
     * 插入一条用户记录
     * @param entity 实体对象
     * @param database 数据库名称
     * @return int
     */
    /*public void baseInsertUsers( WmsWarehouse entity, String database ) throws ServiceException,SucessException{
        Integer i = dao.baseInsertUsers( entity,database );
        if(i>0){
            *//*throw new SucessException("新增用户成功");*//*
        }else{
            throw new ServiceException("新增用户失败");
        }
    }*/
/**
* 插入一条记录（匹配有值的字段）
* @param entity 实体对象
* @return int
*/
public Integer baseInsertSelective( WmsWarehouse entity ){
     return dao.baseInsertSelective( entity );
}

/**
* 删除（根据主键ID删除）
* @param id 主键
* @return int
*/
public Integer baseDeleteByPrimaryKey( String id ){
     return dao.baseDeleteByPrimaryKey( id );
}

/**
* 删除（根据ID 批量删除）
* @param idList 主键ID列表
* @return int
*/
public Integer baseDeleteBathPrimaryKeys( List<String> idList ){
     return dao.baseDeleteBathPrimaryKeys( idList );
}

/**
* 分页查询
* @param entity 实体
* @return Page<WmsWarehouse>
*/
@BaseServiceDescribe(moduleName = "仓库管理",btnName = "分页查询仓库")
public Page<WmsWarehouse> baseSelectPage(WmsWarehouse entity ) throws ServiceException {
    try {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsWarehouse>) dao.baseSelectPage(entity);
    }catch(Exception e){
        throw new ServiceException("查询仓库失败",e);
    }
}

/**
* 查询（根据主键ID查询）
* @param id 主键ID
* @return WmsWarehouse
*/
public WmsWarehouse baseSelectByPrimaryKey(String id ){
    /*List<String> list = dao.baseSelectByPrimaryKeys( mc );*/
    WmsWarehouse wmsWarehouse=dao.baseSelectByPrimaryKey( id );
    /*wmsWarehouse.setIds(list);*/
    return wmsWarehouse;
}

/**
* 查询（根据ID 批量查询）
* @param idList 主键ID列表
* @return list
*/
public Integer baseSelectBathPrimaryKeys( List<WmsWarehouse> idList ){
     return dao.baseSelectBathPrimaryKeys( idList );
}

/**
*修改 （匹配有值的字段）
* @param entity 主键ID列表
* @return int
*/
@BaseServiceDescribe(moduleName = "仓库管理",btnName = "编辑仓库")
public void baseUpdateByPrimaryKeySelective( WmsWarehouse entity ) throws ServiceException{
   //database子库;name主库
   WmsWarehouse entitys = dao.searchADD2(entity);//主库数据库去重
   if (entitys != null) {
       throw new ServiceException("编辑仓库信息重复");
   } else {
       /*SysUser sysUser = dao.searchADDUser2(entity, name);//主库用户查询去重
       SysUser sysUser2 = dao.searchADDUser2(entity, database);//子库用户查询去重
       if (sysUser != null && sysUser2 != null) {
           throw new ServiceException("编辑用户名称重复");
       } else {*/
           Integer zhu = dao.baseUpdateByPrimaryKeySelective(entity);//主库数据库更改
           /*Integer zi = 0;
           Integer gn = 0;
           if (zhu > 0) {
               zi = dao.baseUpdateByPrimaryKeySelective(entity, database);//子库数据库更改
               Integer user = 0;
               if (zi > 0) {
                   user = dao.baseUpdateByPrimaryKeySelective_user(entity, name);//主库用户更改操作
                   Integer user2 = 0;
                   if (user > 0) {
                       user2 = dao.baseUpdateByPrimaryKeySelective_userz(entity, database);//子库用户更改操作
                       if (user2 > 0) {
                           Integer role = dao.deleteRoleFunction(entity.getRole_id(), database);//删除已有角色功能
                           Integer role2 = dao.deleteRoleMenu(entity.getRole_id(), database);//删除已有角色菜单
                           List<String> ids = entity.getIds();
                           List<SysRoleFunction> sysRoleFunction = dao.getFunctionMenu(database);//全部的功能和菜单
                           SysRoleMenu sm;
                           SysRoleFunction syn;
                           for (int i = 0; i < sysRoleFunction.size(); i++) {
                               for (int j = 0; j < ids.size(); j++) {
                                   if (sysRoleFunction.get(i).getGnid().equals(ids.get(j))) {
                                       if (sysRoleFunction.get(i).getType().equals("0")) {
                                           sm = new SysRoleMenu();
                                           sm.setId(UUIDUtils.uuid());
                                           sm.setJsid(entity.getRole_id());
                                           sm.setCdid(ids.get(j));
                                           gn = dao.addRoleMenu(sm, database);
                                           if (gn < 0) {
                                               throw new ServiceException("编辑角色菜单失败");
                                           }
                                       } else {
                                           syn = new SysRoleFunction();
                                           syn.setId(UUIDUtils.uuid());
                                           syn.setJsid(entity.getRole_id());
                                           syn.setGnid(ids.get(j));
                                           gn = dao.addSysRoleFunction(syn, database);
                                           if (gn < 0) {
                                               throw new ServiceException("编辑角色功能失败");
                                           }
                                       }
                                   }

                               }
                           }
                       } else {
                           throw new ServiceException("编辑用户失败");
                       }
                   } else {
                       throw new ServiceException("编辑用户失败");
                   }
               } else {
                   throw new ServiceException("编辑仓库失败");
               }*/
           if(zhu>0){

           } else {
               throw new ServiceException("编辑仓库失败");
           }
   }
}

/**
*修改（根据主键ID修改）
* @param entity 主键ID列表
* @return int
*/
public Integer baseUpdateByPrimaryKey( WmsWarehouse entity ){
     return dao.baseUpdateByPrimaryKey( entity );
}
    /**
     *用户修改
     * @param entity
     * @return
     */
 public  void baseUpdateByPrimaryKeySelective_user(WmsWarehouse entity ) throws ServiceException,SucessException {
     Integer i = dao.baseUpdateByPrimaryKeySelective_user( entity );
     if(i>0){
         /*throw new SucessException("编辑用户成功");*/
     }else{
         throw new ServiceException("编辑用户失败");
     }

 }
    /**
     *用户修改
     * @param entity
     * @return
     */
    public  void baseUpdateByPrimaryKeySelective_userz(WmsWarehouse entity ) throws ServiceException,SucessException{
        Integer i = dao.baseUpdateByPrimaryKeySelective_userz( entity );
        if(i>0){
            /*throw new SucessException("编辑用户成功");*/
        }else{
            throw new ServiceException("编辑用户失败");
        }
    }

    /**
     * 查询角色编号
     * @param mc
     * @return
     */
    public SysRole searchRole(String mc ){
        return dao.searchRole(mc );
    }

    /**
     * 子库添加用户角色数据
     * @param role_id
     * @param User_id
     * @return
     *//*
    public Integer addRoleUser(String role_user,String role_id,String User_id, String database)throws ServiceException,SucessException{
        Integer i= dao.addRoleUser(role_user,role_id, User_id,  database);
        if(i>0){
            *//*throw new SucessException("新增用户角色成功");*//*
        }else{
            throw new ServiceException("新增用户角色失败");
        }
    }*/

    /**
     * 添加前去除重复
     * @param entity
     * @return
     */
    public WmsWarehouse searchADD(WmsWarehouse entity){
        return dao.searchADD(entity);
    }

    /**
     * 查询仓库管理员角色编号
     * @param mc
     * @return
     */
    public SysRole addSearchJs(String mc ){
        return dao.addSearchJs(mc);
    }
    /**
     * 用户添加去除重复
     * @param entity
     * @param
     * @return
     */
    public SysUser searchADDUser(WmsWarehouse entity){
        return dao.searchADDUser(entity);
    }
    /**
     * 查询前台传来的所有菜单和功能集合
     * @return
     */
    public List<SysRoleFunction> getFunctionMenu()throws ServiceException{
        try {
            return dao.getFunctionMenu();
        }catch(Exception e){
            throw new ServiceException("查询菜单和功能集合失败",e);
        }
    }
    /**
     * 保存角色菜单方法
     * @param sysRoleMenu
     * @return
     */
    /*public void addRoleMenu(SysRoleMenu sysRoleMenu, String database)throws ServiceException,SucessException{
        Integer i=dao.addRoleMenu(sysRoleMenu,database);
        if(i>0){
            *//*throw new SucessException("新增角色菜单成功");*//*
        }else{
            throw new ServiceException("新增角色菜单失败");
        }
    }*/
    /**
     * 保存新加的角色功能
     * @param sysRoleFunction
     * @return
     */
    /*public void addSysRoleFunction(SysRoleFunction sysRoleFunction,String database)throws ServiceException,SucessException{
        Integer i= dao.addSysRoleFunction(sysRoleFunction,database);
        if(i>0){
            *//*throw new SucessException("新增角色功能成功");*//*
        }else{
            throw new ServiceException("新增角色功能失败");
        }
    }*/
    /**
     * 删除已有角色功能
     * @param id
     * @return
     */
    public void deleteRoleFunction(String id)throws ServiceException,SucessException{
        Integer i= dao.deleteRoleFunction(id);
        if(i>0){
            /*throw new SucessException("删除角色功能成功");*/
        }else{
            throw new ServiceException("删除角色功能失败");
        }
    }
    /**
     * 删除已有角色菜单
     * @param id
     * @return
     */
    /*public Integer deleteRoleMenu(String id, String database)throws ServiceException{
        Integer i= dao.deleteRoleMenu(id,database);
        if(i<0){
            throw new ServiceException("删除角色菜单失败");
        }
    }*/
}
